package com.rdc.dictionary.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.rdc.dictionary.R;

import com.rdc.dictionary.bean.WordSearch;


import java.util.List;

/**
 * @author Soul Mate
 * @file 文件名
 * @brief 单词历史搜索纪录的adapter
 */

public class WordsSearchListAdapter extends RecyclerView.Adapter<WordsSearchListAdapter.ViewHolder>
{
	private List<WordSearch> mWordsSearchList;

	private OnItemClickListener mListener;



	static class ViewHolder extends RecyclerView.ViewHolder
	{
		//单词名
		TextView wordName;

		//删除
		ImageView delete;

		public ViewHolder(View view)
		{
			super(view);
			wordName = (TextView) view.findViewById(R.id.tv_wordSearch_item);
			delete = (ImageView) view.findViewById(R.id.iv_delete_search);
		}

	}


	//new的时候设置回调
	public WordsSearchListAdapter(List<WordSearch> wordList , OnItemClickListener listener)
	{
		mWordsSearchList = wordList;
		//监听回调
		this.mListener = listener;
	}



	@NonNull @Override public WordsSearchListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
	{
		View view = LayoutInflater.from(parent.getContext())
				.inflate(R.layout.wordslist_item, parent, false);
		WordsSearchListAdapter.ViewHolder holder = new WordsSearchListAdapter.ViewHolder(view);

		//监听单词点击事件
		holder.wordName.setOnClickListener(new View.OnClickListener()
		{
			@Override public void onClick(View v)
			{
				//得到点击位置
				int position =holder.getAdapterPosition();

				mListener.onClickSearch(position);
			}
		});

		//监听删除点击事件
		holder.delete.setOnClickListener(new View.OnClickListener()
		{
			@Override public void onClick(View v)
			{
				//得到点击位置
				int position =holder.getAdapterPosition();

				mListener.onClickDelete(position);
			}
		});

		return holder;
	}





	@Override public void onBindViewHolder(@NonNull WordsSearchListAdapter.ViewHolder holder, int position)
	{

		WordSearch wordSearch = mWordsSearchList.get(position);
		holder.wordName.setText(wordSearch.getName());
	}

	@Override public int getItemCount()
	{
		return mWordsSearchList.size();
	}


	/**
	 * @author Soul Mate
	 * @file 文件名
	 * @brief 点击子项回调的外部接口。在外部定义所需实现的方法
	 */
	public interface OnItemClickListener
	{
		void onClickSearch(int pos);

		void onClickDelete(int pos);
	}

}
